import Vue from 'vue'
import Router from 'vue-router'
import { pageConfig } from '@/config'
import { getCookies, setCookies } from '@/utils/util'
Vue.use(Router)
import authorize from '@/utils/wxAuthorize'

const Home = (resolve) => require(['./views/tab/home.vue'], resolve);
//定义全局域名 在路由当中对各个文件进行限制
export default new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            if (from.meta.keepAlive) {
                from.meta.savedPosition = document.body.scrollTop;
            }
            return { x: 0, y: to.meta.savedPosition || 0 }
        }
    },
    routes: [
        {
            path: '',

            redirect: '/m/enter'
        },
        {
            path: '*',

            redirect: '/m/enter'
        },
        {
            path: '/m/enter',
            component: (resolve) => require(['./views/enter.vue'], resolve),
            meta: {

            },
            children: [
                {
                    path: '',
                    redirect: 'home'
                },
                {
                    name: 'home',
                    path: 'home',
                    component: Home,
                    meta: {
                        showBottomBar: true
                    },
                },

                {
                    name: 'cart',
                    path: 'cart',
                    component: (resolve) => require(['./views/tab/cart.vue'], resolve),
                    meta: {
                        showBottomBar: true
                    }
                },
                {
                    name: 'cate',
                    path: 'cate',
                    component: (resolve) => require(['./views/tab/cate.vue'], resolve),
                    meta: {
                        showBottomBar: true
                    }
                },
                {
                    name: 'my',
                    path: 'my',
                    component: (resolve) => require(['./views/tab/my.vue'], resolve),
                    meta: {
                        showBottomBar: true
                    }
                }
            ]
        },
        // 前二模块
        // home 二级页
        {
            name: 'test',
            path: '/test',
            component: (resolve) => require(['./views/test.vue'], resolve),
            meta: {}
        },
        {
            name: 'freshVegetables',
            path: '/freshVegetables',
            component: (resolve) => require(['./views/home/freshVegetables.vue'], resolve),
            meta: {}
        },
        {
            name: 'meatEgg',
            path: '/meatEgg',
            component: (resolve) => require(['./views/home/meatEgg.vue'], resolve),
            meta: {}
        },
        {
            name: 'grainOil',
            path: '/grainOil',
            component: (resolve) => require(['./views/home/grainOil.vue'], resolve),
            meta: {}
        },
        {
            name: 'newToTry',
            path: '/newToTry',
            component: (resolve) => require(['./views/home/newToTry.vue'], resolve),
            meta: {}
        },
        {
            name: 'orchardGarden',
            path: '/orchardGarden',
            component: (resolve) => require(['./views/home/orchardGarden.vue'], resolve),
            meta: {}
        },
        {
            name: 'oldVip',
            path: '/oldVip',
            component: (resolve) => require(['./views/home/oldVip.vue'], resolve),
            meta: {}
        },
        {
            name: 'rechangeDivision',
            path: '/rechangeDivision',
            component: (resolve) => require(['./views/home/rechageDivision.vue'], resolve),
            meta: {}
        },
        {
            name: 'couponExchange',
            path: '/couponExchange',
            component: (resolve) => require(['./views/home/couponExchange.vue'], resolve),
            meta: {}
        },
        {
            name: 'exchangeSuccess',
            path: '/exchangeSuccess',
            component: (resolve) => require(['./views/home/exchangeSuccess.vue'], resolve),
            meta: {}
        },
        {
            name: 'product_detail',
            path: '/product_detail',
            component: (resolve) => require(['./views/product/product-detail'], resolve),
            meta: {

            }
        },
        // 优惠券
        {
            name: 'discount_coupon',
            path: '/discount_coupon',
            component: (resolve) => require(['./views/home/discount-coupon'], resolve),
            meta: {

            }
        },
        {
            name: 'getCouponSuccess',
            path: '/getCouponSuccess',
            component: (resolve) => require(['./views/home/discount-coupon/getCouponSuccess.vue'], resolve),
            meta: {}
        },
        // 礼品卡
        {
            name: 'gift_card',
            path: '/gift_card',
            component: (resolve) => require(['./views/home/gift-card'], resolve),
            meta: {

            }
        },
        // 订单详情 单个
        {
            name: 'order_detail',
            path: '/order_detail',
            component: (resolve) => require(['./views/paying/order-detail'], resolve),
            meta: {

            }
        },
        // 订单详情 多个
        {
            name: 'order_details2',
            path: '/order_details2',
            component: (resolve) => require(['./views/paying/order-details2'], resolve),
            meta: {

            }
        },
        {
            name: 'search',
            path: '/search',
            component: (resolve) => require(['./views/search'], resolve),
            meta: {

            }
        },
        {
            name: 'product_list',
            path: '/product_list',
            component: (resolve) => require(['./views/product/product-list'], resolve),
            meta: {

            }
        },
        {
            name: 'paying',
            path: '/paying',
            component: (resolve) => require(['./views/paying'], resolve),
            meta: {

            }
        },
        // 后二模块
        {
            name: 'allOrder',
            path: '/allOrder',
            component: (resolve) => require(['./views/my/myAllOrder.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'wallet',
            path: '/wallet',
            component: (resolve) => require(['./views/my/myWallet.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'integral',
            path: '/integral',
            component: (resolve) => require(['./views/my/myIntegral.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'annieBean',
            path: '/annieBean',
            component: (resolve) => require(['./views/my/myAnnieBean.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'annieBeanExchange',
            path: '/annieBeanExchange',
            component: (resolve) => require(['./views/my/myAnnieBeanExchange.vue'], resolve),
            meta: {
            }
        }, 
        {
            name: 'anniBeanLog',
            path: '/anniBeanLog',
            component: (resolve) => require(['./views/my/myAnniBeanLog.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'integralExchangeRecord',
            path: '/integralExchangeRecord',
            component: (resolve) => require(['./views/my/myExchangeRecord.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'myTransactionRecord',
            path: '/myTransactionRecord',
            component: (resolve) => require(['./views/my/myTransactionRecord.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'myCollection',
            path: '/myCollection',
            component: (resolve) => require(['./views/my/myCollection.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'myFeedback',
            path: '/myFeedback',
            component: (resolve) => require(['./views/my/myFeedback.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'myAddressManagement',
            path: '/myAddressManagement',
            component: (resolve) => require(['./views/my/myAddressManagement.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'createAddress',
            path: '/createAddress',
            component: (resolve) => require(['./views/my/createdAddress.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'afterService',
            path: '/afterService',
            component: (resolve) => require(['./views/my/myAfterService.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'bindPhone',
            path: '/bindPhone',
            component: (resolve) => require(['./views/bindPhone/bindPhone.vue'], resolve),
            meta: {
            }
        },
        {
            name:'privateGoods',
            path: '/privateGoods',
            component: (resolve) => require(['./views/my/privateGoods.vue'], resolve),
            meta: {
            }
        },
        {
            name:'activity',
            path: '/activity',
            component: (resolve) => require(['./views/home/activity.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'invoice',
            path: '/invoice',
            component: (resolve) => require(['./views/my/invoice.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'mycoupon',
            path: '/mycoupon',
            component: (resolve) => require(['./views/my/couponList.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'message',
            path: '/public_message',
            component: (resolve) => require(['./views/message/detail.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'messageList',
            path: '/message_list',
            component: (resolve) => require(['./views/message/list.vue'], resolve),
            meta: {
            }
        },
        {
            name: 'vipgrade',
            path: '/vipgrade',
            component: (resolve) => require(['./views/my/vipgrade.vue'], resolve),
            meta: {
            }
        }
    ]
})

// 授权
// router.beforeEach((to, from, next) => {
//     var appid;
//     // 链接后存在openid
//     if (to.query && to.query.openid) {
//         next()
//     } else {
//         // 需要登录的页面
//         if (to.meta.isLogin) {
//             if (to.query.appid) {
//                 appid = to.query.appid;
//                 // 本地无appid
//                 if (!getCookies('appId')) {
//                     setCookies('appId', appid, this.cookieTime)
//                     authorsize(to.path, appid);
//                 } else {
//                     if (Storage.getCookies('appid') == appid) {
//                         setCookies('appId', appid, this.cookieTime);
//                         // 有appid 无openid
//                         if (!getCookies('openId')) {
//                             authorize(to.path, appid)
//                         } else {
//                             if (to.meta.isNeedInfo) {
//                                 // 需要用户数据 验证本地
//                                 // next()
//                             } else {
//                                 next()
//                             }
//                         }
//                     } else {
//                         setCookies('appId', appid, this.cookieTime);
//                         authorize(to.path, appid);
//                     }
//                 }
//             } else {
//                 // 链接内不含appid 跳转错误页
//                 next()
//             }
//         } else {
//             // 不需要登录页面
//             next()
//         }
//     }
// })
